﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Isha.Data.Infrastructure;
using Isha.Entities;
using Isha.Repositories;
using Isha.Repositories.Infrastructure;
using Isha.Services.Infrastructure;

namespace Isha.Services
{
    internal class PartService : ServiceBase, IPartService
    {
        public PartService()
        {
            Initialize(null, null);
        }

        public PartService(IPartRepository partRepository, IUnitOfWork unitOfWork)
        {
            Initialize(partRepository, unitOfWork);
        }

        public IPartRepository PartRepository { get; private set; }

        public IUnitOfWork UnitOfWork { get; private set; }

        public void Initialize(IPartRepository partRepository, IUnitOfWork unitOfWork)
        {
            this.PartRepository = (partRepository ?? DependencyResolver.Current.GetService<IPartRepository>());
            this.UnitOfWork = (unitOfWork ?? DependencyResolver.Current.GetService<IUnitOfWork>());
        }

        public IEnumerable<Part> GetParts()
        {
            var parts = PartRepository.GetAll();
            return parts;
        }

        public Part GetPart(long id)
        {
            var part = PartRepository.Find(id);
            return part;
        }

        public void CreatePart(Part part)
        {
            PartRepository.Add(part);
            UnitOfWork.Commit();
        }

        public void DeletePart(long id)
        {
            PartRepository.Delete(id);
            UnitOfWork.Commit();
        }


    }
}
